<template>
  <el-dialog :title="'店铺价格调整'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" :inline="true">
      <el-form-item label="商品名称">
        <span>{{ goods.goodsName }}</span>
      </el-form-item>
      <el-form-item
        v-for="(price, index) in dataForm.priceList"
        :key="index"
        :label="price.shopName + '：'"
      >
        <el-form-item label="商品价格" :prop="`priceList.${index}.goodsPrice`" :rules="checkPriceRule">
          <el-input v-model="price.goodsPrice" placeholder="商品单价"></el-input>
        </el-form-item>
        <el-form-item
          label="折扣率"
          :prop="`priceList.${index}.goodsDiscount`"
          :rules="checkDiscountRule"
        >
          <el-input v-model="price.goodsDiscount" placeholder="折扣率1-100" maxlength="3"></el-input>
        </el-form-item>
      </el-form-item>
      <!-- <el-form-item
        v-for="(price, index) in dataForm.priceList"
        :key="index"
        :label="price.shopName"
        :prop="`priceList.${index}.goodsPrice`"
        :rules="checkPriceRule"
      >
        <el-input v-model="price.goodsPrice" placeholder="商品单价"></el-input>
      </el-form-item>-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" :disabled="disabled" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    var checkPrice = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请填写价格"));
      }
      if (!/^\d+(\.\d{0,2})?$/.test(value)) {
        callback(new Error("请填写正确的价格"));
      } else if (value <= 0) {
        callback(new Error("价格必须大于0"));
      } else {
        callback();
      }
    };
    var checkDiscount = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请填写折扣率"));
      }
      if (!/^([1-9][0-9]{0,1}|100)$/.test(value)) {
        callback(new Error("请输入1-100之间的整数"));
      } else if (value <= 0) {
        callback(new Error("请输入1-100之间的整数"));
      } else {
        callback();
      }
    };
    return {
      visible: false,
      disabled: false,
      dataForm: {
        priceList: []
      },
      goods: {
        id: null,
        goodsName: ""
      },
      dataRule: {},
      checkPriceRule: [{ validator: checkPrice, trigger: "blur" }],
      checkDiscountRule: [{ validator: checkDiscount, trigger: "blur" }]
    };
  },
  methods: {
    init(id) {
      this.goods = {
        id: id || null,
        goodsName: ""
      };
      this.dataForm = {
        priceList: []
      };
      this.$nextTick(() => {
        if (id) {
          const loading = this.$loading({
            lock: true,
            text: "Loading",
            spinner: "el-icon-loading",
            background: "rgba(0, 0, 0, 0.7)"
          });
          this.$http({
            url: this.$http.adornUrl(`/merchant/goods/shop-price/${id}`),
            method: "get",
            params: this.$http.adornParams()
          })
            .then(({ data }) => {
              loading.close();
              if (data && data.code === 0) {
                this.goods = data.data.goods;
                this.dataForm.priceList = data.data.priceList;
                this.visible = true;
              }
            })
            .catch(() => {
              loading.close();
            });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.disabled = true;
          this.$http({
            url: this.$http.adornUrl(`/merchant/goods/save-shop-price`),
            method: "post",
            data: this.$http.adornData({
              priceList: this.dataForm.priceList
            })
          })
            .then(({ data }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.visible = false;
                    this.disabled = false;
                    // this.$emit('refreshDataList');
                  }
                });
              } else {
                this.$message.error(data.msg);
              }
            })
            .catch(() => {
              this.disabled = false;
            });
        }
      });
    }
  }
};
</script>
